<template>
	<el-card class="box-card">
		<div slot="header" class="clearfix">
			<span>用户列表</span>
		</div>
		<el-table :data="tableData">
			<el-table-column prop="user_id" label="#"></el-table-column>
			<el-table-column prop="username" label="用户名"></el-table-column>
			<el-table-column prop="nickname" label="昵称"></el-table-column>
			<el-table-column prop="sex" label="性别"></el-table-column>
			<el-table-column prop="tel" label="手机"></el-table-column>
			<el-table-column prop="email" label="邮箱" width="200"></el-table-column>
			<el-table-column label="头像">
				<template slot-scope="scope">
					<el-avatar shape="square" :src="scope.row.avatar"></el-avatar>
				</template>
			</el-table-column>
			<el-table-column label="状态">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.usable === 1" type="success">启用</el-tag>
					<el-tag v-if="scope.row.usable !== 1" type="danger">禁用</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="300">
				<template slot-scope="scope">
					<el-button v-permission="'user:list:switch'" v-if="scope.row.usable === 1" type="info" plain
						icon="el-icon-lock" @click="handleBanned(scope.row.user_id, scope.$index)">禁用</el-button>
					<el-button v-permission="'user:list:switch'" v-if="scope.row.usable !== 1" type="success" plain
						icon="el-icon-key" @click="handleOpen(scope.row.user_id, scope.$index)">启用</el-button>
					<el-button v-permission="'user:list:remove'" type="danger" plain icon="el-icon-delete"
						@click="handleRemove(scope.row.user_id, scope.$index)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
	</el-card>
</template>

<script>
import { User } from '@/api';
export default {
	data() {
		return {
			tableData: [],
		}
	},
	// 在created或mounted发送ajax请求(通过调用函数)
	created() {
		document.title = "用户列表页";
		this.loadList();
	},
	methods: {
		// 一般将发送AJAX写到methods中
		// 加载列表
		async loadList() {
			let { status, msg, data } = await User.list({ pagesize: 12, pageindex: 1 });
			if (status) {
				this.tableData = data;
			} else {
				this.$message.error(msg);
			}
		},
		// 禁用用户
		handleBanned(user_id, index) {
			this.$confirm('该操作将禁用该用户, 是否继续?', '提示', { type: 'warning' })
				.then(async () => {
					let { status, msg } = await User.usable({ user_id, usable: -1 });
					if (status) {
						// 操作DOM
						this.tableData[index].usable = -1;
						// 信息提示
						this.$message.success(msg);
					} else {
						this.$message.error(msg);
					}
				}).catch(() => {
					this.$message.info("已取消该操作!");
				});
		},
		// 启用用户
		handleOpen(user_id, index) {
			this.$confirm('该操作将启用该用户, 是否继续?', '提示', { type: 'warning' })
				.then(async () => {
					let { status, msg } = await User.usable({ user_id, usable: 1 });
					if (status) {
						// 操作DOM
						this.tableData[index].usable = 1;
						// 信息提示
						this.$message.success(msg);
					} else {
						this.$message.error(msg);
					}
				}).catch(() => {
					this.$message.info("已取消该操作！");
				});
		},
		// 删除
		handleRemove(user_id, index) {
			this.$confirm('该操作将删除该用户, 是否继续?', '提示', { type: 'warning' })
				.then(async () => {
					let { status, msg } = await User.remove({ user_id });
					if (status) {
						// 操作DOM
						this.tableData.splice(index, 1);
						// 信息提示
						this.$message.success(msg);
					} else {
						this.$message.error(msg);
					}
				}).catch(() => {
					this.$message.info("已取消该操作！");
				});
		},
	},
}
</script>

<style lang="less" scoped>
.el-tag {
	margin-right: 10px;
}
</style>
